package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.VideoCardDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyWorksListFragment extends BaseListFragment<MyWorksListFragment.SocietyUserDynamicViewHolder, VideoCard> {
    Long uid = null;

    public static MyWorksListFragment create(Long uid) {
        MyWorksListFragment fragment = new MyWorksListFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("uid", uid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uid = getArguments().getLong("uid", 0l);
    }

    public MyWorksListFragment() {
    }

    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<VideoCard>>(new TypeReference<Result<List<VideoCard>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }

            @Override
            public void responseInfo(Result<List<VideoCard>> data) {
                addResult(data);
            }
        });
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
        return layoutManager;
    }

    /**
     * @param cardid
     */
    public void forwardDetail(Long cardid) {
        Intent intent = new Intent(this.getActivity(), VideoCardDetailActivity.class);
        intent.putExtra("cardid", cardid);
        startActivity(intent);
    }


    @Override
    public int getSpanSize() {
        return 3;
    }

    @Override
    protected SocietyUserDynamicViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_user_detail_card, parent, false);
        SocietyUserDynamicViewHolder viewHolder = new SocietyUserDynamicViewHolder(view, new DetailVideoListener() {
            @Override
            public void videoCardClick(VideoCard card) {
                forwardDetail(card.cardid);
            }
        });
        return viewHolder;
    }

    @Override
    protected SocietyUserDynamicViewHolder createfootViewHolder(View view) {
        return new SocietyUserDynamicViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<VideoCard> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_my_video_card, uid, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }

    public interface DetailVideoListener {
        public void videoCardClick(VideoCard card);

    }

    public static class SocietyUserDynamicViewHolder extends BaseRecycleViewHolder<VideoCard> {
        //视频封面
        private NetworkImageView mVideoThum;
        //视频描述
        private TextView mVideoDesc;
        //点赞
        private TextView mZan;
        //评论
        private TextView mComment;

        private View itemView;

        private DetailVideoListener listener;

        public SocietyUserDynamicViewHolder(View itemView, DetailVideoListener listener) {
            this(itemView);
            this.listener = listener;
        }

        public void initWidget(View itemView) {
            this.itemView = itemView;
            mVideoThum = (NetworkImageView) itemView.findViewById(R.id.niv_video_thum);
            mVideoDesc = (TextView) itemView.findViewById(R.id.tv_video_desc);
            mZan = (TextView) itemView.findViewById(R.id.tv_zan);
            mComment = (TextView) itemView.findViewById(R.id.tv_comment);
        }

        public SocietyUserDynamicViewHolder(View itemView) {
            super(itemView);
            initWidget(itemView);
        }

        @Override
        public void renderView(final VideoCard data, int position) {
            mVideoThum.setImageUrl(data.video.thumUrl, HttpUtils.getImageLoader());
            mVideoDesc.setText(data.description);
            mZan.setText(data.videoAttach.praisenum + "");
            mComment.setText(data.videoAttach.replynum + "");
            this.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.videoCardClick(data);
                }
            });

        }

    }
}
